import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/sliver_list_widget.dart';
import 'package:jetour_line/pages/baseWidget/bottom_widget.dart';
import 'package:jetour_line/pages/common/commonDetail/common_detail_head_widget.dart';
import 'package:jetour_line/pages/common/commonDetail/common_detail_list_widget.dart';
import 'package:jetour_line/utils/extension_int.dart';

import '../../../map/map_utils.dart';
import '../../../models/common_detail_model.dart';
import '../../../my_colors.dart';
import '../../../network/jetour_http.dart';
import 'dart:convert';

class CommonDetailScreen extends StatefulWidget {
  final int id;
  Function? collectCallBack;

  CommonDetailScreen({Key? key, required this.id, this.collectCallBack})
      : super(key: key);

  @override
  State<CommonDetailScreen> createState() => _CommonDetailScreenState();
}

class _CommonDetailScreenState extends State<CommonDetailScreen> {
  CommonDetailModel? detailModel;
  bool isCollect = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDetail();
    MapUtils.getMaps();
  }

  Future<bool> _onWillPop() async {
    if (widget.collectCallBack != null) {
      widget.collectCallBack!(isCollect);
    }
    Navigator.pop(context);
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Platform.isIOS
        ? GestureDetector(
            onHorizontalDragUpdate: (details) {
              if (details.delta.dx > 0) {
                _onWillPop!.call();
              }
            },
            child: WillPopScope(
              onWillPop: _onWillPop,
              child: getBodyWidget(),
            ),
          )
        : WillPopScope(
            onWillPop: _onWillPop,
            child: getBodyWidget(),
          );
  }

  Widget getBodyWidget() {
    return BaseScaffold(
        title: '共创路书详情',
        leftItemCallBack: () {
          if (widget.collectCallBack != null) {
            widget.collectCallBack!(isCollect);
          }
          Navigator.pop(context);
        },
        body: detailModel == null
            ? const SizedBox()
            : Column(
                children: [
                  Expanded(
                    child: CustomScrollView(
                      slivers: [
                        CommonDetailHeadWidget(
                          detailModel: detailModel!,
                        ),
                        SliverListWidget.getSliverContainer(
                            120.px,
                            Column(
                              children: [
                                SizedBox(
                                  width: double.infinity,
                                  height: 24.px,
                                ),
                                Container(
                                  padding: EdgeInsets.only(left: 24.px),
                                  width: double.infinity,
                                  height: 96.px,
                                  color: MyColors.white,
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    "行程线路",
                                    style: TextStyle(
                                        fontSize: 32.px,
                                        color: MyColors.color1A1A1A,
                                        fontWeight: FontWeight.w500),
                                  ),
                                )
                              ],
                            )),
                        CommonDetailListWidget(detailModel: detailModel!)
                      ],
                    ),
                  ),
                  BottomWidget(
                    type: 2,
                    typeId: detailModel!.id,
                    shareTitle: detailModel!.caption,
                    shareSubTitle: detailModel!.description,
                    shareImgUrl: detailModel!.images.isEmpty
                        ? ''
                        : detailModel!.images.first,
                    fav: detailModel!.otherData.fav,
                    callBack: () {
                      getDetail();
                    },
                    collectCallBack: (isCo) {
                      isCollect = isCo;
                    },
                  )
                ],
              ));
  }

  void getDetail() async {
    JetourHttp().get(JetourRouter.CommonDetail, params: {"id": widget.id}).then(
        (result) {
      //256
      detailModel = CommonDetailModel.fromJson(result);
      setState(() {});
    }).catchError((e) {});
  }
}
